package com.weiyu.hbase.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weiyu.hbase.entity.HBaseColumn;
import com.weiyu.hbase.entity.HBaseColumnFamily;
import com.weiyu.hbase.entity.HBaseRow;
import com.weiyu.hbase.entity.MonitorData;
import com.weiyu.hbase.utils.HBaseUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/hbase")
public class IndexController {


    @GetMapping("/record/{table}/{row}/{family}/{column}")
    public String selectValue(@PathVariable(value = "table") String table, @PathVariable(value = "row") String row, @PathVariable(value = "family") String family, @PathVariable(value = "column") String column) {
        String result = "";
        try {
            result = HBaseUtils.getInstance().getColumn(table, row, family, column);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/record/{table}/{row}/{family}")
    public List selectByColumnFamily(@PathVariable(value = "table") String table, @PathVariable(value = "row") String row, @PathVariable(value = "family") String family) {
        List<HBaseColumn> resultList = new ArrayList<HBaseColumn>();
        try {
            resultList = HBaseUtils.getInstance().getByColumnFamily(table, row, family);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultList;
    }

    @GetMapping("/record/{table}/{row}")
    public List selectRow(@PathVariable(value = "table") String table, @PathVariable(value = "row") String row) {
        List<HBaseColumnFamily> list = new ArrayList<HBaseColumnFamily>();
        try {
            list = HBaseUtils.getInstance().getByRow(table, row);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    @GetMapping("/record/{table}")
    public List selectRow(@PathVariable(value = "table") String table) {
        List<HBaseRow> list = new ArrayList<HBaseRow>();
        try {
            Long startime = System.currentTimeMillis();
            list = HBaseUtils.getInstance().getByTable(table);
            Long endtime = System.currentTimeMillis();
            System.out.println("耗时：" + (endtime - startime) );
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    @GetMapping("/scanAllRecord/{table}")
    public String scanAllRecord(@PathVariable(value = "table") String table) {
        String result = "";
        try {
            result = HBaseUtils.getInstance().scanAllRecord(table);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/checkFamily/{table}/{family}")
    public Boolean scanAllRecord(@PathVariable(value = "table") String table , @PathVariable(value = "family") String family ) {
        Boolean flag = Boolean.FALSE;
        try {
            flag = HBaseUtils.getInstance().isExistFamily(table,family);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @PostMapping("/addColumnFamily")
    public String addColumnFamily(@RequestBody HBaseColumn HBaseColumn) {
        String result = "";
        try {
            HBaseUtils.getInstance().addColumnFamily(HBaseColumn.getTableName(), HBaseColumn.getColumnFamilyName());
            result = "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/insertOneRecord")
    public String insertOneRecord(@RequestBody HBaseColumn HBaseColumn) {
        String result = "";
        try {
            HBaseUtils.getInstance().insertOneRecord(HBaseColumn.getTableName(), HBaseColumn.getRowName(), HBaseColumn.getColumnFamilyName(), HBaseColumn.getColumnName(), HBaseColumn.getValue());
            result = "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/createTable/{table}")
    public String createTable(@PathVariable(value = "table") String table) {
        String result = "";
        try {
            String[] str = {"monitor_data","hello"};
            HBaseUtils.getInstance().createTable(table,str);
            result = "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @DeleteMapping("/deleteTable/{table}")
    public String deleteTable(@PathVariable(value = "table") String table) {
        String result = "";
        try {
            HBaseUtils.getInstance().deleteTable(table);
            result = "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @DeleteMapping("/deleteRow/{table}/{row}")
    public String deleteRow(@PathVariable(value = "table") String table, @PathVariable(value = "row") String row) {
        String result = "";
        try {
            HBaseUtils.getInstance().deleteRow(table, row);
            result = "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @DeleteMapping("/deleteColumnFamily/{table}/{row}/{family}")
    public String deleteColumnFamily(@PathVariable(value = "table") String table, @PathVariable(value = "row") String row, @PathVariable(value = "family") String family) {
        String result = "";
        try {
            HBaseUtils.getInstance().deleteColumnFamily(table, row, family);
            result = "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    ObjectMapper mapper = new ObjectMapper();

    @PostMapping("/insertRecords")
    public String insertRecords(@RequestBody /*MonitorData[] data */List<MonitorData> data) {
        String result = "";
        try {
            String json = mapper.writeValueAsString(data);
            System.out.println(json);
            Long startime = System.currentTimeMillis();
            HBaseUtils.getInstance().insertOneRecord("abner","4444","mdata", "9999",json);
            Long endtime = System.currentTimeMillis();
            Long cost = endtime - startime;
            System.out.println("耗时:" + cost );
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    @PostMapping("/batchPut")
    public String batchPut(@RequestBody  MonitorData[] data) {
        try {
            Long cost = new Long(0);
            List puts = new ArrayList();
            for(Integer i=0;i<data.length;i++){
                String json = mapper.writeValueAsString(data[i]);
                Put put = new Put(Bytes.toBytes("3444"));
                put.addColumn(Bytes.toBytes("mdata"), Bytes.toBytes(i.toString()), Bytes.toBytes(json));
                puts.add(put);
            }
            System.out.println("耗时:" + cost );
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";

    }

}
